package com.rykj.energy.task.manage;

import com.rykj.energy.common.dto.TaskCameraRelationDto;
import com.rykj.energy.common.enums.TaskState;
import com.rykj.energy.common.enums.TaskType;
import com.rykj.energy.common.utils.CollectionUtils;
import com.rykj.energy.common.utils.JsonUtil;
import com.rykj.energy.common.utils.StringUtils;
import com.rykj.energy.task.manage.actuator.ITaskManage;
import com.rykj.energy.task.manage.schedule.IQuartzService;
import com.rykj.energy.task.manage.schedule.ManualCaptureJob;
import com.rykj.energy.task.manage.vo.ExecPlanDto;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDataMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 无任务有效期
 */
@Service
@Slf4j
public class TaskManageFactorySchedule {

    @Autowired
    private ITaskManage videoTaskManage;

    @Autowired
    private ITaskManage captureTaskManage;

    @Autowired
    private IQuartzService quartzService;

    public boolean startTask(List<TaskCameraRelationDto> voList) throws Exception{

        List<TaskCameraRelationDto> videoList = new ArrayList<>();
        for(TaskCameraRelationDto relationDto : voList){
            //添加执行计划
            String jName = (relationDto.getCameraId()) + "-" + (relationDto.getPresetId()==null?"0":relationDto.getPresetId()) + "-" + (relationDto.getTaskCode());
            String jGroup = relationDto.getTaskType();
            //{"time":{"second":"60"}}
            String execPlan = relationDto.getExecPlan();
            if(relationDto.getTaskType().equals(TaskType.video.getCode())){
                videoList.add(relationDto); //等待开始的视频分析任务
            }
            if(StringUtils.isBlank(execPlan)){
               if(relationDto.getTaskType().equals(TaskType.capture.getCode())){
                   throw new Exception("图片分析必须设置执行计划!!");
               }
            } else {
                ExecPlanDto planDto = JsonUtil.stringToBean(execPlan,ExecPlanDto.class);
                if(relationDto.getTaskType().equals(TaskType.capture.getCode())){
                    JobDataMap jobDataMap = new JobDataMap();
                    jobDataMap.put("cameraId",relationDto.getCameraId());
                    jobDataMap.put("taskId",relationDto.getTaskId());
                    jobDataMap.put("taskCode",relationDto.getTaskCode());
                    jobDataMap.put("presetId",relationDto.getPresetId());
                    jobDataMap.put("taskType",relationDto.getTaskType());
                    quartzService.addJobSeconds(ManualCaptureJob.class,jName,jGroup,planDto.getTime().getSecond(),jobDataMap);
                }
            }
        }

        if(videoList.size()>0){
            videoTaskManage.startTask(videoList);
        }
        return true;
    }

    public boolean stopTask(List<TaskCameraRelationDto> voList) throws Exception{

        List<TaskCameraRelationDto> videoList = CollectionUtils.newInstanceList();
        List<TaskCameraRelationDto> captureList = CollectionUtils.newInstanceList();

        for(TaskCameraRelationDto relationDto : voList){
            //删除执行计划
            String jName = (relationDto.getCameraId()) + "-" + (relationDto.getPresetId()==null?"0":relationDto.getPresetId()) + "-" + (relationDto.getTaskCode());
            String jGroup = relationDto.getTaskType();
            quartzService.deleteJob(jName,jGroup);
            if(relationDto.getStatus().equals(TaskState.run.getCode())){ //运行中的任务需停止计算
                //停止任务
                if(relationDto.getTaskType().equals(TaskType.video.getCode())){
                    videoList.add(relationDto);
                }
                if(relationDto.getTaskType().equals(TaskType.capture.getCode())){
                    captureList.add(relationDto);
                }
            }
        }

        if(videoList.size()>0){
            videoTaskManage.stopTask(videoList);
        }
        if(captureList.size()>0){
            captureTaskManage.stopTask(captureList);
        }
        return true;
    }

    public boolean updateTask(List<TaskCameraRelationDto> voList) throws Exception{
        List<TaskCameraRelationDto> videoList = new ArrayList<>();
        for(TaskCameraRelationDto relationDto : voList){
            //添加执行计划
            String jName = relationDto.getCameraId() + "-" + relationDto.getPresetId()==null?"0":relationDto.getPresetId() + "-" + relationDto.getTaskCode();
            String jGroup = relationDto.getTaskType();
            //{"time":{"second":"60"}}
            String execPlan = relationDto.getExecPlan();
            if(relationDto.getTaskType().equals(TaskType.video.getCode())){
                videoList.add(relationDto); //等待开始的视频分析任务
            }
            if(StringUtils.isBlank(execPlan)){
                if(relationDto.getTaskType().equals(TaskType.capture.getCode())){
                    throw new Exception("图片分析必须设置执行计划!!");
                }
            } else {
                ExecPlanDto planDto = JsonUtil.stringToBean(execPlan,ExecPlanDto.class);
                if(relationDto.getTaskType().equals(TaskType.capture.getCode())){
                    JobDataMap jobDataMap = new JobDataMap();
                    jobDataMap.put("cameraId",relationDto.getCameraId());
                    jobDataMap.put("taskId",relationDto.getTaskId());
                    jobDataMap.put("taskCode",relationDto.getTaskCode());
                    jobDataMap.put("presetId",relationDto.getPresetId());
                    jobDataMap.put("taskType",relationDto.getTaskType());
                    quartzService.addJobSeconds(ManualCaptureJob.class,jName,jGroup,planDto.getTime().getSecond(),jobDataMap);
                }
            }
        }

        if(videoList.size()>0){
            videoTaskManage.updateTask(videoList);
        }
        return true;
    }
}
